package cn.lmxdev.ptms.service.impl;

import cn.lmxdev.ptms.bean.exception.BizException;
import cn.lmxdev.ptms.bean.table.TSpecialtyTeacher;
import cn.lmxdev.ptms.dao.ISpecialtyTeacherDao;
import cn.lmxdev.ptms.service.ISpecialtyTeacherServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SpecialtyTeacherServletImpl implements ISpecialtyTeacherServlet {
    private final ISpecialtyTeacherDao specialtyTeacherDao;

    @Autowired
    public SpecialtyTeacherServletImpl(
            ISpecialtyTeacherDao specialtyTeacherDao
    ) {
        this.specialtyTeacherDao = specialtyTeacherDao;
    }

    @Override
    @Transactional
    public TSpecialtyTeacher get(Long id) {
        return this.specialtyTeacherDao.find(id);
    }

    @Override
    @Transactional
    public long add(TSpecialtyTeacher specialtyTeacher) {
        // 插入数据
        if (!this.specialtyTeacherDao.insert(specialtyTeacher))
            throw new BizException("操作失败");

        // 返回ID
        return specialtyTeacher.getId();
    }

    @Override
    @Transactional
    public void remove(Long id) {
        // 删除数据
        if (!this.specialtyTeacherDao.delete(id))
            throw new BizException("操作失败");
    }

    @Override
    @Transactional
    public void edit(TSpecialtyTeacher specialtyTeacher) {
        // 查询旧数据
        TSpecialtyTeacher oldSpecialtyTeacher = this.specialtyTeacherDao.find(specialtyTeacher.getId());

        // 更新属性
        oldSpecialtyTeacher.setAdviserId(specialtyTeacher.getAdviserId())
                .setOfficial(specialtyTeacher.getOfficial());

        // 保存数据
        if (!this.specialtyTeacherDao.update(oldSpecialtyTeacher))
            throw new BizException("操作失败");
    }

    @Override
    @Transactional
    public long count(TSpecialtyTeacher specialtyTeacher) {
        return this.specialtyTeacherDao.count(specialtyTeacher);
    }

    @Override
    @Transactional
    public List<TSpecialtyTeacher> list(Long limit, Long page, TSpecialtyTeacher specialtyTeacher) {
        return this.specialtyTeacherDao.select(page != null ? (page - 1) * limit : null, limit, specialtyTeacher);
    }

    @Override
    @Transactional
    public long specialtyHeadCount(Long specialtyHeadId, TSpecialtyTeacher specialtyTeacher) {
        return this.specialtyTeacherDao.specialtyHeadCount(specialtyHeadId, specialtyTeacher);
    }

    @Override
    @Transactional
    public List<TSpecialtyTeacher> specialtyHeadList(Long specialtyHeadId, Long limit, Long page, TSpecialtyTeacher specialtyTeacher) {
        return this.specialtyTeacherDao.specialtyHeadSelect(specialtyHeadId, page != null ? (page - 1) * limit : null, limit, specialtyTeacher);
    }
}
